<?php 
session_cache_expire(999999999);
if(!isset($_SESSION)) 
{ 
	session_start(); 
}


//$_SESSION['searchload'] = $_REQUEST['employeename'];
//$_SESSION['loadCheckTimes'] = $objects;
	
	$lang = $_SESSION['lang'];
	if (empty($lang) || $lang == "EN") {
		include "../value/value_en.php";
	} else if($lang == "VN") {
		include "../value/value_vn.php";
	}
	else {
		include "../value/value_kr.php";
	}

include("../content_salary/sa_funcs.php");
$mthu=array();
$mthu[0]=$l_vday_chunhat;
$mthu[1]=$l_vday_thuhai;
$mthu[2]=$l_vday_thuba;
$mthu[3]=$l_vday_thutu;
$mthu[4]=$l_vday_thunam;
$mthu[5]=$l_vday_thusau;
$mthu[6]=$l_vday_thubay;
$mthu[7]='';
if (isset($_SESSION['loadCheckTimes'])) {
	include 'CheckTime.php';
	//include '../Classes/PHPExcel.php';
	/** Include path **/
	set_include_path(get_include_path() . PATH_SEPARATOR . 'ClassesExcel/');
	
	/** PHPExcel */
	require_once 'PHPExcel.php';
	
	/** PHPExcel_RichText */
	require_once 'PHPExcel/RichText.php';
	
	$filename =$l_working_spreadsheet."_".$_SESSION['searchload']['employeename'].".xls";
	$result = mysql_query($query);
	if ($_SESSION['searchload']['employeename']=="") {
		$filename =$l_working_spreadsheet.".xls";
	}
	
	$contents = "Name \t ID \t Date \t In \t Out \t Late \t Early \t Off half day \t Off a day \t Overtime \t Note \t \n";
	
	$objPHPExcel = new PHPExcel();
	$objPHPExcel->setActiveSheetIndex(0);
	
	/*$objPHPExcel->getProperties()->setCreator('Matthew Casperson')
	->setLastModifiedBy('Matthew Casperson')
	->setTitle('Office 2007 XLSX Test Document')
	->setSubject('Office 2007 XLSX Test Document')
	->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.');*/
	$styleArray = array(
		'font' => array(
			'bold' => true,
			'size' => 11,
		),
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		'fill' => array(
			'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
			'rotation' => 90,
			'startcolor' => array(
				'argb' => 'FFA0A0A0',
			),
			'endcolor' => array(
				'argb' => 'FFFFFFFF',
			),
		),
	);
	$orange = array(
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		),
		'fill' => array(
			'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
			'rotation' => 90,'startcolor' => array(
				'argb' => 'FFFFA603',
			),
			'endcolor' => array(
				'argb' => 'FFFFFFFF',
			),
		),
	);
	$yellow = array(
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		),
		'fill' => array(
			'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
			'rotation' => 90,'startcolor' => array(
				'argb' => 'FFFCE803',
			),
			'endcolor' => array(
				'argb' => 'FFFFFFFF',
			),
		),
	);
	$pink = array(
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		),
		'fill' => array(
			'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
			'rotation' => 90,'startcolor' => array(
				'argb' => 'FFA0A0A0',
			),
			'endcolor' => array(
				'argb' => 'FFFCBDC8',
			),
		),
	);
	$red = array(
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		),
		'fill' => array(
			'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
			'rotation' => 90,'startcolor' => array(
				'argb' => 'FFFF0000',
			),
			'endcolor' => array(
				'argb' => 'FFFFFFFF',
			),
		),
	);
	$green = array(
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		),
		'fill' => array(
			'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
			'rotation' => 90,'startcolor' => array(
				'argb' => 'FF00FF00',
			),
			'endcolor' => array(
				'argb' => 'FF038803',
			),
		),
	);
	
	$styleArray2 = array(
			'alignment' => array(
							'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
							'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
							'wrap' => true,
						),
			'borders' => array(
							'top' => array(
								'style' => PHPExcel_Style_Border::BORDER_THIN,
							),
							'bottom' => array(
								'style' => PHPExcel_Style_Border::BORDER_THIN,
							),
							'left' => array(
								'style' => PHPExcel_Style_Border::BORDER_THIN,
							),
							'right' => array(
								'style' => PHPExcel_Style_Border::BORDER_THIN,
							),
						),
			
		);
	
	$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
	$objPHPExcel->getActiveSheet()->mergeCells('A2:L2');
	$objPHPExcel->getActiveSheet()->getStyle('A2')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->setCellValue('A2', $l_working_spreadsheet);
	
	$objPHPExcel->getActiveSheet()->freezePane('A5');
	$objPHPExcel->getActiveSheet()->getRowDimension('4')->setRowHeight(20);
	$objPHPExcel->getActiveSheet()->getStyle('A4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('A4', $l_employee_name);
	
	$objPHPExcel->getActiveSheet()->getStyle('B4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('B4', $l_code);
	
	$objPHPExcel->getActiveSheet()->getStyle('C4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('C4', $l_thu);
	
	$objPHPExcel->getActiveSheet()->getStyle('D4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('D4', $l_date);
	
	$objPHPExcel->getActiveSheet()->getStyle('E4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('E4', $l_timecheckin);
	
	$objPHPExcel->getActiveSheet()->getStyle('F4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('F4', $l_timecheckout);
	
	$objPHPExcel->getActiveSheet()->getStyle('G4')->applyFromArray($styleArray);
	//$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('G4', $l_late);
	
	$objPHPExcel->getActiveSheet()->getStyle('H4')->applyFromArray($styleArray);
	//$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('H4', $l_comebackearly);
	
	$objPHPExcel->getActiveSheet()->getStyle('I4')->applyFromArray($styleArray);
	//$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('I4', $l_half_dayoff);
	
	$objPHPExcel->getActiveSheet()->getStyle('J4')->applyFromArray($styleArray);
	//$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('J4', $l_all_dayoff);
	
	$objPHPExcel->getActiveSheet()->getStyle('K4')->applyFromArray($styleArray);
	//$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('K4', $l_overtime);
	
	$objPHPExcel->getActiveSheet()->getStyle('L4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('L4', $l_note);
	//->setCellValue('L4', $l_update_information);

	foreach ($_SESSION['loadCheckTimes'] as $key =>  $object){
		$CT = unserialize (serialize ($object));
		$thu=getWday($CT->date);
		
		$key = $key+5;
		
		$objPHPExcel->getActiveSheet()->getRowDimension($key)->setRowHeight(20);
		
		$objPHPExcel->getActiveSheet()->getStyle('A'.$key)->applyFromArray($styleArray2);
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$key, $CT->employeeName);
		
		$objPHPExcel->getActiveSheet()->getStyle('B'.$key)->applyFromArray($styleArray2);
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$key, $CT->employeeID);
		
		$objPHPExcel->getActiveSheet()->getStyle('C'.$key)->applyFromArray($styleArray2);
		$objPHPExcel->getActiveSheet()->setCellValue('C'.$key, $mthu[$thu]);
		
		$objPHPExcel->getActiveSheet()->getStyle('D'.$key)->applyFromArray($styleArray2);
		$objPHPExcel->getActiveSheet()->setCellValue('D'.$key, ($CT->date!="0") ? $CT->date : "");
		
		$objPHPExcel->getActiveSheet()->getStyle('E'.$key)->applyFromArray($styleArray2);
		$objPHPExcel->getActiveSheet()->setCellValue('E'.$key, ($CT->inTime!="0") ? $CT->inTime : "");
		
		$objPHPExcel->getActiveSheet()->getStyle('F'.$key)->applyFromArray($styleArray2);
		$objPHPExcel->getActiveSheet()->setCellValue('F'.$key, ($CT->outTime!="0") ? $CT->outTime : "");
		
		$objPHPExcel->getActiveSheet()->getStyle('G'.$key)->applyFromArray($styleArray2);
		if($CT->late!=0){
		$objPHPExcel->getActiveSheet()->getStyle('G'.$key)->applyFromArray($orange);
		$objPHPExcel->getActiveSheet()->setCellValue('G'.$key, ($CT->late!="0") ? "X" : "");
		}
		
		$objPHPExcel->getActiveSheet()->getStyle('H'.$key)->applyFromArray($styleArray2);
		if($CT->early!=0){
		$objPHPExcel->getActiveSheet()->getStyle('H'.$key)->applyFromArray($yellow);
		$objPHPExcel->getActiveSheet()->setCellValue('H'.$key, ($CT->early!="0") ? "X" : "");
		}
		
		$objPHPExcel->getActiveSheet()->getStyle('I'.$key)->applyFromArray($styleArray2);
		if($CT->halfDayOff!=0){
		$objPHPExcel->getActiveSheet()->getStyle('I'.$key)->applyFromArray($pink);
		$objPHPExcel->getActiveSheet()->setCellValue('I'.$key, ($CT->halfDayOff!="0") ? "X" : "");
		}
		
		$objPHPExcel->getActiveSheet()->getStyle('J'.$key)->applyFromArray($styleArray2);
		if($CT->aDayOff!=0){
		$objPHPExcel->getActiveSheet()->getStyle('J'.$key)->applyFromArray($red);
		$objPHPExcel->getActiveSheet()->setCellValue('J'.$key, ($CT->aDayOff!="0") ? "X" : "");
		}
		
		$objPHPExcel->getActiveSheet()->getStyle('K'.$key)->applyFromArray($styleArray2);
		if($CT->overtime!=0){
			$str="";
			for ($i=0;$i<$CT->overtime;$i++){
					$str.= 'X';
			}
		$objPHPExcel->getActiveSheet()->getStyle('K'.$key)->applyFromArray($green);
		$objPHPExcel->getActiveSheet()->setCellValue('K'.$key, ($CT->overtime!="0") ? $str : "");
		}
		
		$objPHPExcel->getActiveSheet()->getStyle('L'.$key)->applyFromArray($styleArray2);
		$objPHPExcel->getActiveSheet()->setCellValue('L'.$key, ($CT->note!="0") ? $CT->note : "");
	//	->setCellValue('L'.$key, $CT->update_user."/n".$CT->update_time);
		/*$contents.= $CT->employeeName. " \t";
		 $contents.= $CT->employeeID. " \t";
		 $contents.= $CT->date. " \t";
		 $contents.= $CT->inTime. " \t";
		 $contents.= $CT->outTime. " \t";
		 $contents.= $CT->late. " \t";
		 $contents.= $CT->early. " \t";
		 $contents.= $CT->halfDayOff. " \t";
		 $contents.= $CT->aDayOff. " \t";
		 $contents.= $CT->overtime. " \t";
		 $contents.= $CT->note. " \n";*/
	}
	
	include 'PHPExcel/IOFactory.php';
	
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	header("Content-type: application/vnd.ms-excel");
	header('Content-Disposition: attachment;filename="' . $filename);
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	$objWriter->save('php://output');
}
exit;

?>